//
//  KKDataBaseAPIInterface.m
//  FreightAssistant
//
//  Created by Mac mini on 15/12/15.
//  Copyright © 2015年 leo. All rights reserved.
//

#import "KKDataBaseAPIInterface.h"

#import "KKDataBaseManager.h"

@implementation KKDataBaseAPIInterface

+ (void)editItemInfoWithRecords:(NSDictionary *)records withTableName:(NSString *)tableName withIdentifier:(NSString *)identifier successful:(KKDataBaseAPIInterfaceSuccessfulBlock)success faile:(KKDataBaseAPIInterfaceFaileBlock)faile {
    
    if (tableName == nil || records.count == 0) {
        return;
    }
    
    if (identifier == nil) {
        //  新增
        
        NSMutableDictionary *newRecords = [NSMutableDictionary dictionaryWithDictionary: records];
        [newRecords setValue: @"1" forKey: @"enabled"];
        
        [[KKDataBaseManager defaultManager] insertRecordIntoTableName: tableName withRecords: newRecords finishHandle:^(id object, BOOL successful) {
            
            if (successful) {
                success(@{@"identifier":object});
            }
            else {
                if (faile) {
                    faile(@"数据库操作错误");
                }
                [self showDataBaseError: @"数据库操作失败"];
            }
        }];
    }
    else {
        //  编辑
        [[KKDataBaseManager defaultManager] updateRecordWithTableName: tableName withRecords: records withCondition: @{@"identifier":identifier} finishHandle:^(id object, BOOL successful) {
            if (successful) {
                
                if (success) {
                    if ([object isKindOfClass: [NSString class]]) {
                        success(@{@"identifier":object});
                    }
                    else {
                        success(object);
                    }
                }
            }
            else {
                if (faile) {
                    faile(@"数据库操作错误");
                }
                [self showDataBaseError: @"数据库操作失败"];
            }
        }];
    }
}

+ (void)deleteItemWithIdentifier:(NSString *)identifier WithTableName:(NSString *)tableName successful:(KKDataBaseAPIInterfaceSuccessfulBlock)success faile:(KKDataBaseAPIInterfaceFaileBlock)faile {
    
    if (tableName == nil || identifier == nil) {
        return;
    }
    
    NSMutableDictionary *records = [NSMutableDictionary dictionary];
    [records setValue: @"0" forKey: @"enabled"];
    
    NSMutableDictionary *condition = [NSMutableDictionary dictionary];
    [condition setValue: identifier forKey: @"identifier"];
    
    [[KKDataBaseManager defaultManager] updateRecordWithTableName: tableName withRecords: records withCondition: condition finishHandle:^(id object, BOOL successful) {
        NSLog(@"更新：object = %@ | successful = %i", object, successful);
        
        if (successful) {
            if (success) {
                success(nil);
            }
        }
        else {
            if (faile) {
                faile(@"数据库操作失败");
            }
            
            [self showDataBaseError: @"数据库操作失败"];
        }
    }];
}

+ (void)searchItemsWithTableName:(NSString *)tableName successful:(KKDataBaseAPIInterfaceSuccessfulBlock)success faile:(KKDataBaseAPIInterfaceFaileBlock)faile {
    
    if (tableName == nil) {
        return;
    }
    
    NSMutableDictionary *conditions = [NSMutableDictionary dictionary];
    [conditions setValue: @"1" forKey: @"enabled"];
    
    [[KKDataBaseManager defaultManager] searchRecordWithTableName: tableName withCondition: conditions finishHandle:^(id object, BOOL successful) {
        if (successful) {
            if (success) {
                success(object);
            }
        }
        else {
            if (faile) {
                faile(@"数据库操作失败");
            }
            
            [self showDataBaseError: @"数据库操作失败"];
        }
    }];
}

+ (void)showDataBaseError:(NSString *)error {
    
    [[KKResultAlertView sharedInstance] alertViewShowMessage: @"error" andWithSuccessful: false andWithCallBack: nil];
}

+ (NSDictionary*)conditionsWithEnabled:(BOOL)enabled searchText:(NSString *)searchText {
    
    NSMutableDictionary *conditions = [NSMutableDictionary dictionary];
    
    if (enabled) {
        [conditions setValue: @"1" forKey: @"enabled"];
    }
    
    if (searchText) {
        [conditions setValue: searchText forKey: @"name"];
    }
    
    return conditions;
}

#pragma mark - Public methods
#pragma mark - Commodity methods

+ (void)addCommodityWithRecords:(NSDictionary *)records successful:(KKDataBaseAPIInterfaceSuccessfulBlock)success faile:(KKDataBaseAPIInterfaceFaileBlock)faile {
    
    [self editItemInfoWithRecords: records withTableName: KKDataBase_TableName_CommodityInfo withIdentifier: nil successful: success faile: faile];
}

+ (void)editCommodityWithRecords:(NSDictionary *)records withCommodityIdentifier:(NSString *)identifier successful:(KKDataBaseAPIInterfaceSuccessfulBlock)success faile:(KKDataBaseAPIInterfaceFaileBlock)faile {
    
    [self editItemInfoWithRecords: records withTableName: KKDataBase_TableName_CommodityInfo withIdentifier: identifier successful: success faile: faile];
}

+ (void)searchCommodityItemsWithSearchText:(NSString *)searchText withEnabled:(BOOL)enabled successful:(KKDataBaseAPIInterfaceSuccessfulBlock)success faile:(KKDataBaseAPIInterfaceFaileBlock)faile {
    
    NSDictionary *conditions = [self conditionsWithEnabled: enabled searchText: searchText];
    
    NSArray *keyArray = @[@"identifier",@"name",@"purchasePrice",@"price",@"inventory",@"capacity",@"sales",@"enabled"];
    
    [[KKDataBaseManager defaultManager] executeSQLStringWithTableName: KKDataBase_TableName_CommodityInfo withValueKeys: keyArray withCondition: conditions finishHandle:^(id object, BOOL successful) {
        if (successful) {
            if (success) {
                success(object);
            }
        }
        else {
            if (faile) {
                faile(@"数据库操作失败");
            }
            
            [self showDataBaseError: @"数据库操作失败"];
        }
    }];
}

+ (void)deleteCommodityItemWithIdentifier:(NSString *)identifier successful:(KKDataBaseAPIInterfaceSuccessfulBlock)success faile:(KKDataBaseAPIInterfaceFaileBlock)faile {
    
    [self deleteItemWithIdentifier: identifier WithTableName: KKDataBase_TableName_CommodityInfo successful: success faile: faile];
}


#pragma mark - Customer methods

+ (void)addCustomerWithRecords:(NSDictionary *)records successful:(KKDataBaseAPIInterfaceSuccessfulBlock)success faile:(KKDataBaseAPIInterfaceFaileBlock)faile {
    
    [self editItemInfoWithRecords: records withTableName: KKDataBase_TableName_CustomerInfo withIdentifier: nil successful: success faile: faile];
}

+ (void)editCustomerWithRecords:(NSDictionary *)records withCommodityIdentifier:(NSString *)identifier successful:(KKDataBaseAPIInterfaceSuccessfulBlock)success faile:(KKDataBaseAPIInterfaceFaileBlock)faile {
    
    [self editItemInfoWithRecords: records withTableName: KKDataBase_TableName_CustomerInfo withIdentifier: identifier successful: success faile: faile];
}

+ (void)searchCustomerItemsWithSearchText:(NSString *)searchText withEnabled:(BOOL)enabled successful:(KKDataBaseAPIInterfaceSuccessfulBlock)success faile:(KKDataBaseAPIInterfaceFaileBlock)faile {
    
    NSDictionary *conditions = [self conditionsWithEnabled: enabled searchText: searchText];
    
    NSArray *keyArray = @[@"identifier",@"name",@"phone",@"address",@"enabled"];
    
    [[KKDataBaseManager defaultManager] executeSQLStringWithTableName: KKDataBase_TableName_CustomerInfo withValueKeys: keyArray withCondition: conditions finishHandle:^(id object, BOOL successful) {
        if (successful) {
            if (success) {
                success(object);
            }
        }
        else {
            if (faile) {
                faile(@"数据库操作失败");
            }
            
            [self showDataBaseError: @"数据库操作失败"];
        }
    }];
}

+ (void)deleteCustomerItemWithIdentifier:(NSString *)identifier successful:(KKDataBaseAPIInterfaceSuccessfulBlock)success faile:(KKDataBaseAPIInterfaceFaileBlock)faile {
    
    [self deleteItemWithIdentifier: identifier WithTableName: KKDataBase_TableName_CustomerInfo successful: success faile: faile];
}


#pragma mark - Logistics methods

+ (void)addLogisticsWithRecords:(NSDictionary *)records successful:(KKDataBaseAPIInterfaceSuccessfulBlock)success faile:(KKDataBaseAPIInterfaceFaileBlock)faile {
    
    [self editItemInfoWithRecords: records withTableName: KKDataBase_TableName_LogisticsInfo withIdentifier: nil successful: success faile: faile];
}

+ (void)editLogisticsWithRecords:(NSDictionary *)records withCommodityIdentifier:(NSString *)identifier successful:(KKDataBaseAPIInterfaceSuccessfulBlock)success faile:(KKDataBaseAPIInterfaceFaileBlock)faile {
    
    [self editItemInfoWithRecords: records withTableName: KKDataBase_TableName_LogisticsInfo withIdentifier: identifier successful: success faile: faile];
}

+ (void)searchLogisticsItemsWithSearchText:(NSString *)searchText withEnabled:(BOOL)enabled successful:(KKDataBaseAPIInterfaceSuccessfulBlock)success faile:(KKDataBaseAPIInterfaceFaileBlock)faile {
    
    NSDictionary *conditions = [self conditionsWithEnabled: enabled searchText: searchText];
    
    NSArray *keyArray = @[@"identifier",@"name",@"code",@"phone",@"enabled"];
    
    [[KKDataBaseManager defaultManager] executeSQLStringWithTableName: KKDataBase_TableName_LogisticsInfo withValueKeys: keyArray withCondition: conditions finishHandle:^(id object, BOOL successful) {
        if (successful) {
            if (success) {
                success(object);
            }
        }
        else {
            if (faile) {
                faile(@"数据库操作失败");
            }
            
            [self showDataBaseError: @"数据库操作失败"];
        }
    }];
}

+ (void)deleteLogisticsItemWithIdentifier:(NSString *)identifier successful:(KKDataBaseAPIInterfaceSuccessfulBlock)success faile:(KKDataBaseAPIInterfaceFaileBlock)faile {
    
    [self deleteItemWithIdentifier: identifier WithTableName: KKDataBase_TableName_LogisticsInfo successful: success faile: faile];
}


+ (void)addPurchaseOrderWithOrderRecords:(NSDictionary *)orderRecords customerIdentifier:(NSString *)customerIdentifier commodityItems:(NSArray *)commodityItems logisticsRecords:(NSDictionary*)logisticsRecords successful:(KKDataBaseAPIInterfaceSuccessfulBlock)success faile:(KKDataBaseAPIInterfaceFaileBlock)faile{
    
    
    //  生成订单数据
    [self editItemInfoWithRecords: orderRecords withTableName: KKDataBase_TableName_PurchaseOrder withIdentifier: nil successful:^(id object) {
        
        KKDataBaseManager *baseManager = [KKDataBaseManager defaultManager];
        
        //  获取订单号
        if ([object isKindOfClass: [NSDictionary class]]) {
            NSDictionary *dictionary = (NSDictionary*)object;
            NSString *orderIdentifier = dictionary[@"identifier"];
            
            NSMutableArray *sqlStrings = [NSMutableArray array];
            //物流信息
            NSMutableDictionary *orderLogisticsRecords = [NSMutableDictionary dictionaryWithDictionary: logisticsRecords];
            [orderLogisticsRecords setValue: orderIdentifier forKey: @"orderId"];
            
            NSString *orderLogisticsSQL = [baseManager sqlStringOfInsertRecordIntoTableName: KKDataBase_TableName_PurchaseOrder_Logistics withRecords: orderLogisticsRecords sqlIdentifier: nil];
            [sqlStrings addObject: orderLogisticsSQL];
            
            //客户信息
            NSMutableDictionary *customerRecords = [NSMutableDictionary dictionary];
            [customerRecords setValue: customerIdentifier forKey: @"customerId"];
            [customerRecords setValue: orderIdentifier forKey: @"orderId"];
            
            NSString *orderCustomrSQL = [baseManager sqlStringOfInsertRecordIntoTableName: KKDataBase_TableName_PurchaseOrder_Customer withRecords: customerRecords sqlIdentifier: nil];
            [sqlStrings addObject: orderCustomrSQL];
            
            //商品信息入库
            
            for (NSInteger index = 0; index < commodityItems.count; index++) {
                
                NSDictionary *commodityItemDictionary = commodityItems[index];
                NSString *commodityItemIdentifier;
                NSString *commodityItemSQL = [baseManager sqlStringOfInsertRecordIntoTableName: KKDataBase_TableName_PurchaseCommodityInfo withRecords: commodityItemDictionary sqlIdentifier: &commodityItemIdentifier];
                
                NSString *orderCommoditySQL = [baseManager sqlStringOfInsertRecordIntoTableName: KKDataBase_TableName_PurchaseOrder_CommodityInfo withRecords: @{@"orderId": orderIdentifier,@"commodityId": commodityItemIdentifier} sqlIdentifier: nil];
                
                [sqlStrings addObject: commodityItemSQL];
                [sqlStrings addObject: orderCommoditySQL];
            }
            
            [baseManager execInsertTransactionSQLStringArray: sqlStrings];
        }
        
        if (success) {
            success(object);
        }
        
    } faile: nil];
}

+ (void)updatePurchaseOrderStateWithIdentifier:(NSString *)identifier state:(NSInteger)state successful:(KKDataBaseAPIInterfaceSuccessfulBlock)success faile:(KKDataBaseAPIInterfaceFaileBlock)faile {
    
    KKDataBaseManager *dataBaseManager = [KKDataBaseManager defaultManager];
    
    [dataBaseManager updateRecordWithTableName: KKDataBase_TableName_PurchaseOrder withRecords: @{@"state":@(state)} withCondition: @{@"identifier":identifier} finishHandle:^(id object, BOOL successful) {
        NSLog(@"object = %@", object);
        
        if (successful) {
            
            if (success) {
                success(object);
            }
        }
        else {
            faile(@"操作失败");
        }
        
    }];
}

+ (void)searchPurchaseOrderItemsWithEnabled:(BOOL)enabled successful:(KKDataBaseAPIInterfaceSuccessfulBlock)success faile:(KKDataBaseAPIInterfaceFaileBlock)faile{
    
    KKDataBaseManager *dataBaseManager = [KKDataBaseManager defaultManager];
    
    [dataBaseManager searchRecordWithTableName: KKDataBase_TableName_PurchaseOrder withCondition: @{@"enabled":@(enabled)} finishHandle:^(id object, BOOL successful) {
        
        if ([object isKindOfClass: [NSArray class]]) {
            
            NSMutableArray *resultItemArray = [NSMutableArray array];
            
            NSArray *responseItems = (NSArray*)object;
            for (NSDictionary *itemDictionary in responseItems) {
                
                NSMutableDictionary *itemResultDictionary = [NSMutableDictionary dictionaryWithDictionary: itemDictionary];
                
                NSString *identifier = [itemDictionary valueForKey: @"identifier"];
                
                if (identifier == nil) {
                    continue;
                }
                
                //  订单中的商品
                {
                    NSString *sqlString = [NSString stringWithFormat: @"select KK_PurchaseCommodityInfo.commodityId, KK_PurchaseCommodityInfo.name, KK_PurchaseCommodityInfo.price, KK_PurchaseCommodityInfo.currency, KK_PurchaseCommodityInfo.number,KK_PurchaseCommodityInfo.amount from KK_PurchaseOrder_CommodityInfo left join KK_PurchaseCommodityInfo on KK_PurchaseCommodityInfo.identifier = KK_PurchaseOrder_CommodityInfo.commodityId where KK_PurchaseOrder_CommodityInfo.orderId='%@'", identifier];
                    
                    NSArray *commodityList = [dataBaseManager executeSQLString: sqlString withValueKeys: @[@"commodityId",@"name",@"price",@"currency",@"number",@"amount"]];
                    
                    //                    NSLog(@"result = %@", commodityList);
                    [itemResultDictionary setValue: commodityList forKey: @"purchaseCommodityItems"];
                }
                
                //  客户信息
                {
                    NSString *sqlString = [NSString stringWithFormat: @"select KK_CustomerInfo.name,KK_CustomerInfo.phone,KK_CustomerInfo.address from KK_CustomerInfo left join KK_PurchaseOrder_Customer on KK_CustomerInfo.identifier = KK_PurchaseOrder_Customer.customerId where KK_PurchaseOrder_Customer.orderId = '%@'", identifier];
                    
                    NSArray *customerResult = [dataBaseManager executeSQLString: sqlString withValueKeys: @[@"name",@"phone",@"address"]];
                    
                    //                    NSLog(@"logisticsResult = %@", customerResult);
                    if ([customerResult count]) {
                        [itemResultDictionary setValue: [customerResult lastObject] forKey: @"customerItem"];
                    }
                }
                
                //  物流信息
                {
                    NSString *sqlString = [NSString stringWithFormat: @"select KK_LogisticsInfo.identifier,KK_LogisticsInfo.name, KK_LogisticsInfo.code, KK_PurchaseOrder_Logistics.number, KK_PurchaseOrder_Logistics.amount from KK_PurchaseOrder_Logistics left join KK_LogisticsInfo on KK_PurchaseOrder_Logistics.logisticsId = KK_LogisticsInfo.identifier where KK_PurchaseOrder_Logistics.orderId='%@'", identifier];
                    
                    NSArray *logisticsResult = [dataBaseManager executeSQLString: sqlString withValueKeys: @[@"identifier",@"name",@"code",@"number",@"amount"]];
                    
                    //                    NSLog(@"logisticsResult = %@", logisticsResult);
                    
                    if ([logisticsResult count]) {
                        [itemResultDictionary setValue: [logisticsResult lastObject] forKey: @"logisticsItem"];
                    }
                }
                
                [resultItemArray addObject: itemResultDictionary];
            }
            
            if (success) {
                success(resultItemArray);
            }
        }
        else {
            if (faile) {
                faile(@"查询错误");
            }
        }
    }];
    
}

+ (void)deletePurchaseOrderItemWithIdentifier:(NSString *)identifier successful:(KKDataBaseAPIInterfaceSuccessfulBlock)success faile:(KKDataBaseAPIInterfaceFaileBlock)faile {
    
     [self deleteItemWithIdentifier: identifier WithTableName: KKDataBase_TableName_LogisticsInfo successful: success faile: faile];
}



+ (void)addSupplyOrderWithOrderRecords:(NSDictionary *)orderRecords customerIdentifier:(NSString *)customerIdentifier commodityItems:(NSArray *)commodityItems logisticsRecords:(NSDictionary*)logisticsRecords successful:(KKDataBaseAPIInterfaceSuccessfulBlock)success faile:(KKDataBaseAPIInterfaceFaileBlock)faile {
    
    //  生成订单数据
    [self editItemInfoWithRecords: orderRecords withTableName: KKDataBase_TableName_SupplyOrder withIdentifier: nil successful:^(id object) {
        
        KKDataBaseManager *baseManager = [KKDataBaseManager defaultManager];
        
        //  获取订单号
        if ([object isKindOfClass: [NSDictionary class]]) {
            NSDictionary *dictionary = (NSDictionary*)object;
            NSString *orderIdentifier = dictionary[@"identifier"];
            
            NSMutableArray *sqlStrings = [NSMutableArray array];
            //物流信息
            NSMutableDictionary *orderLogisticsRecords = [NSMutableDictionary dictionaryWithDictionary: logisticsRecords];
            [orderLogisticsRecords setValue: orderIdentifier forKey: @"orderId"];
            
            NSString *orderLogisticsSQL = [baseManager sqlStringOfInsertRecordIntoTableName: KKDataBase_TableName_SupplyOrder_Logistics withRecords: orderLogisticsRecords sqlIdentifier: nil];
            [sqlStrings addObject: orderLogisticsSQL];
            
            //客户信息
            NSMutableDictionary *customerRecords = [NSMutableDictionary dictionary];
            [customerRecords setValue: customerIdentifier forKey: @"customerId"];
            [customerRecords setValue: orderIdentifier forKey: @"orderId"];
            
            NSString *orderCustomrSQL = [baseManager sqlStringOfInsertRecordIntoTableName: KKDataBase_TableName_SupplyOrder_Customer withRecords: customerRecords sqlIdentifier: nil];
            [sqlStrings addObject: orderCustomrSQL];
            
            //商品信息入库
            
            for (NSInteger index = 0; index < commodityItems.count; index++) {
                
                NSDictionary *commodityItemDictionary = commodityItems[index];
                NSString *commodityItemIdentifier;
                NSString *commodityItemSQL = [baseManager sqlStringOfInsertRecordIntoTableName: KKDataBase_TableName_SupplyCommodityInfo withRecords: commodityItemDictionary sqlIdentifier: &commodityItemIdentifier];
                
                NSString *orderCommoditySQL = [baseManager sqlStringOfInsertRecordIntoTableName: KKDataBase_TableName_SupplyOrder_CommodityInfo withRecords: @{@"orderId": orderIdentifier,@"commodityId": commodityItemIdentifier} sqlIdentifier: nil];
                
                [sqlStrings addObject: commodityItemSQL];
                [sqlStrings addObject: orderCommoditySQL];
            }
            
            [baseManager execInsertTransactionSQLStringArray: sqlStrings];
        }
        
        if (success) {
            success(object);
        }
        
    } faile: nil];
}

+ (void)updateSupplyOrderStateWithIdentifier:(NSString *)identifier state:(NSInteger)state successful:(KKDataBaseAPIInterfaceSuccessfulBlock)success faile:(KKDataBaseAPIInterfaceFaileBlock)faile {
    
    KKDataBaseManager *dataBaseManager = [KKDataBaseManager defaultManager];
    
    [dataBaseManager updateRecordWithTableName: KKDataBase_TableName_SupplyOrder withRecords: @{@"state":@(state)} withCondition: @{@"identifier":identifier} finishHandle:^(id object, BOOL successful) {
        if (successful) {
            
            if (success) {
                success(object);
            }
        }
        else {
            faile(@"操作失败");
        }
    }];
}

+ (void)searchSupplyOrderItemsWithEnabled:(BOOL)enabled successful:(KKDataBaseAPIInterfaceSuccessfulBlock)success faile:(KKDataBaseAPIInterfaceFaileBlock)faile {
    
    KKDataBaseManager *dataBaseManager = [KKDataBaseManager defaultManager];
    
    [dataBaseManager searchRecordWithTableName: KKDataBase_TableName_SupplyOrder withCondition: @{@"enabled":@(enabled)} finishHandle:^(id object, BOOL successful) {
        
        if ([object isKindOfClass: [NSArray class]]) {
            
            NSMutableArray *resultItemArray = [NSMutableArray array];
            
            NSArray *responseItems = (NSArray*)object;
            for (NSDictionary *itemDictionary in responseItems) {
                
                NSMutableDictionary *itemResultDictionary = [NSMutableDictionary dictionaryWithDictionary: itemDictionary];
                
                NSString *identifier = [itemDictionary valueForKey: @"identifier"];
                
                if (identifier == nil) {
                    continue;
                }

                //  订单中的商品
                {
                    NSString *sqlString = [NSString stringWithFormat: @"select KK_SupplyCommodityInfo.commodityId, KK_SupplyCommodityInfo.name, KK_SupplyCommodityInfo.price, KK_SupplyCommodityInfo.discount, KK_SupplyCommodityInfo.number from KK_SupplyOrder_CommodityInfo left join KK_SupplyCommodityInfo on KK_SupplyCommodityInfo.identifier = KK_SupplyOrder_CommodityInfo.commodityId where KK_SupplyOrder_CommodityInfo.orderId='%@'", identifier];
                    
                    NSArray *commodityList = [dataBaseManager executeSQLString: sqlString withValueKeys: @[@"commodityId",@"name",@"price",@"discount",@"number"]];
                    
//                    NSLog(@"result = %@", commodityList);
                    [itemResultDictionary setValue: commodityList forKey: @"suppplyCommodityItems"];
                }
                
                //  客户信息
                {
                    NSString *sqlString = [NSString stringWithFormat: @"select KK_CustomerInfo.name,KK_CustomerInfo.phone,KK_CustomerInfo.address from KK_CustomerInfo left join KK_SupplyOrder_Customer on KK_CustomerInfo.identifier = KK_SupplyOrder_Customer.customerId where KK_SupplyOrder_Customer.orderId = '%@'", identifier];
                    
                    NSArray *customerResult = [dataBaseManager executeSQLString: sqlString withValueKeys: @[@"name",@"phone",@"address"]];
                    
//                    NSLog(@"logisticsResult = %@", customerResult);
                    if ([customerResult count]) {
                        [itemResultDictionary setValue: [customerResult lastObject] forKey: @"customerItem"];
                    }
                }
                
                //  物流信息
                {
                    NSString *sqlString = [NSString stringWithFormat: @"select KK_LogisticsInfo.identifier,KK_LogisticsInfo.name, KK_LogisticsInfo.code, KK_SupplyOrder_Logistics.number, KK_SupplyOrder_Logistics.amount from KK_SupplyOrder_Logistics left join KK_LogisticsInfo on KK_SupplyOrder_Logistics.logisticsId = KK_LogisticsInfo.identifier where KK_SupplyOrder_Logistics.orderId='%@'", identifier];
                    
                    NSArray *logisticsResult = [dataBaseManager executeSQLString: sqlString withValueKeys: @[@"identifier",@"name",@"code",@"number",@"amount"]];
                    
//                    NSLog(@"logisticsResult = %@", logisticsResult);
                    
                    if ([logisticsResult count]) {
                        [itemResultDictionary setValue: [logisticsResult lastObject] forKey: @"logisticsItem"];
                    }
                }
                
                [resultItemArray addObject: itemResultDictionary];
            }
            
            if (success) {
                success(resultItemArray);
            }
        }
        else {
            if (faile) {
                faile(@"查询错误");
            }
        }
    }];
}

@end
